package com.iotechn.unimall.admin.api.examineAndApprove;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dobbinsoft.fw.core.exception.AdminServiceException;
import com.dobbinsoft.fw.core.exception.ServiceException;
import com.dobbinsoft.fw.support.model.Page;
import com.iotechn.unimall.data.domain.ExamineAndApproveDO;
import com.iotechn.unimall.data.domain.UserDO;
import com.iotechn.unimall.data.enums.ExamineAndApproveType;
import com.iotechn.unimall.data.enums.UserStatusType;
import com.iotechn.unimall.data.exception.ExceptionDefinition;
import com.iotechn.unimall.data.mapper.ExamineAndApproveMapper;
import com.iotechn.unimall.data.mapper.UserMapper;
import com.iotechn.unimall.data.vo.ExamineAndApproveVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

@Service
public class ExamineAndApproveServiceImpl extends ServiceImpl<ExamineAndApproveMapper, ExamineAndApproveDO> implements ExamineAndApproveService {
    @Resource
    private UserMapper userMapper;

    @Override
    public Page<ExamineAndApproveVO> page(Integer page, Integer limit, Integer status, Integer type, String phone, String startTime, String endTime, Long adminId) throws ServiceException {
        return baseMapper.pageExamineAndApprove(Page.div(page, limit, ExamineAndApproveVO.class), phone, status, type, startTime, endTime);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String update(Long id, Integer type, Integer status, String notPassCause, Long adminId) throws ServiceException {
        ExamineAndApproveDO examineAndApproveDO = new ExamineAndApproveDO();
        examineAndApproveDO.setId(id);
        if (ObjectUtil.isNotEmpty(status)) {
            examineAndApproveDO.setStatus(status);
        }
        if (ObjectUtil.isNotEmpty(status)) {
            examineAndApproveDO.setNotPassCause(notPassCause);
        }
        examineAndApproveDO.setApprover(adminId);
        examineAndApproveDO.setGmtUpdate(new Date());
        if (baseMapper.updateById(examineAndApproveDO) <= 0) {
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }

        //修改用户跟协会绑定
        //查询用户
        QueryWrapper<ExamineAndApproveDO> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(ExamineAndApproveDO::getId, id);
        ExamineAndApproveDO examineAndApprove = baseMapper.selectOne(wrapper);
        QueryWrapper<UserDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(UserDO::getId, examineAndApprove.getUserId());
        UserDO userDO = userMapper.selectOne(queryWrapper);

        //修改用户信息
        if (ExamineAndApproveType.TYPE_JOIN.getCode().equals(type) && ExamineAndApproveType.PASS.getCode().equals(status)) {
            userDO.setAssociationId(examineAndApprove.getAssociationId());
        } else if (ExamineAndApproveType.TYPE_CANCEL.getCode().equals(type) && ExamineAndApproveType.PASS.getCode().equals(status)) {
            userDO.setAssociationId(-1L);
        }
        userDO.setStatus(UserStatusType.ACTIVE.getCode());
        if(userMapper.updateById(userDO) <= 0){
            throw new AdminServiceException(ExceptionDefinition.ADMIN_UNKNOWN_EXCEPTION);
        }
        return "ok";
    }
}
